#include "BaseClient.h"
#include "TcpClient.h"
#include <thread>

using namespace BORNS;

namespace CURCONTROL {

BaseClient::BaseClient():
    tcpClient_(nullptr)
{

}

BaseClient::~BaseClient()
{

}

void BaseClient::EnableReceiveThread()
{
    if(tcpClient_)
        tcpClient_->EnableReceiveThread();
}

bool BaseClient::Connect(const std::string &host, int port, bool checkSelectTimeoutError)
{
    if(!tcpClient_){
        tcpClient_.reset(new TcpClient{checkSelectTimeoutError});
    }
    if(tcpClient_){
        if(tcpClient_->Connect(host, port)){
            return true;
        }
    }
    return false;
}

void BaseClient::Disconnect()
{
    if(tcpClient_){
        tcpClient_->Disconnect();
    }
}

bool BaseClient::Reconnect()
{
    this->Clear();
    if(tcpClient_){
        if(tcpClient_->Reconnect()){
            return true;
        }
    }
    return false;
}

bool BaseClient::IsConnected() const
{
    if(tcpClient_ && tcpClient_->GetState() == SocketState::Connected){
        return true;
    }
    return false;
}

bool BaseClient::StartReceiveThread(std::function<void (std::vector<uint8_t> &)> parseFunc)
{
    if(tcpClient_){
        if(tcpClient_->StartReceiveThread(parseFunc)){
            return true;
        }
    }
    return false;
}

std::string BaseClient::GetIP() const
{
    if(tcpClient_){
        return tcpClient_->GetIP();
    }
    return "";
}

//void BaseClient::SetConnectStatusChangedCallback(std::function<void (bool status)> statusChangedFunc)
//{
//    if(tcpClient_){
//        tcpClient_->SetConnectStatusChangedCallback(statusChangedFunc);
//    }
//}

}// namespace CURCONTROL
